//
//  WDGoutMaintenanceVehServiceInfoView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2025/3/27.
//

#import "WDGoutMaintenanceVehServiceInfoView.h"
#import "WDArrivalServiceDetailModel.h"
#import "WDWorderOrdMainVehSingleImageView.h"
#import "WDWorderOrdMainVehOtherImageVideoView.h" // 其它
#import "HZPhotoBrowser.h"


@interface WDGoutMaintenanceVehServiceInfoView ()
@property (nonatomic, strong) WDArrivalServiceDetailModel *taskDetailModel;

/// 预约时间
@property (nonatomic, strong) WDTaskDetailBasicSingleView *appointTimeView;

/// 抢单时间
@property (nonatomic, strong) WDTaskDetailBasicSingleView *robOrderTimeView;

/// 抢单账号
@property (nonatomic, strong) WDTaskDetailBasicSingleView *robOrderAccountView;

/// 接车时间
@property (nonatomic, strong) WDTaskDetailBasicSingleView *meetCarTimeView;

/// 扫码位置
@property (nonatomic, strong) WDTaskDetailBasicSingleView *scanLocationView;

/// 扫码账户
@property (nonatomic, strong) WDTaskDetailBasicSingleView *scanAccountView;

@property (nonatomic, strong) CALayer *lineOne;

/// 建议处理方式
@property (nonatomic, strong) WDTaskDetailBasicSingleView *processModeView;

/// 建议处理说明
@property (nonatomic, strong) WDTaskDetailBasicSingleView *processSpecView;

/// 备注图片
@property (nonatomic, strong) UILabel *remarkImgTitleLabel;


/// 车辆VIN码照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *vehVinImgView;

/// 车辆仪表照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *vehDashImgView;

/// 车头45°照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *vehHeaderImgView;

/// 故障部位照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *vehFaultyImgView;

/// 驾驶证信息照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *drivingLicenceImgView;

/// 其它
@property (nonatomic, strong) WDWorderOrdMainVehOtherImageVideoView *otherImageVideoView;

/// 处理时间
@property (nonatomic, strong) WDTaskDetailBasicSingleView *processTimeView;

/// 处理账号
@property (nonatomic, strong) WDTaskDetailBasicSingleView *processAccountView;
@end

@implementation WDGoutMaintenanceVehServiceInfoView

/// 预约时间
- (void)initWithAppointTimeView {
    if (!_appointTimeView) {
        CGRect frame = CGRectMake(0, 0, self.width, kWDTaskDetailBasicSingleViewHeight);
        _appointTimeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_appointTimeView];
    }
    if (self.taskDetailModel) {
        NSString *str = objectNull(self.taskDetailModel.appointmentTime);
        if ([str isEqualToString:@""]) {
            str = objectNull(self.taskDetailModel.createTime);
        }
        [_appointTimeView updateWithViewTitle:@"预约时间" value:str valueHeight:18];
    }
}

/// 抢单时间
- (void)initWithRobOrderTimeView {
    if (!_robOrderTimeView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.appointTimeView.bottom;
        _robOrderTimeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_robOrderTimeView];
    }
    if (self.taskDetailModel) {
        [_robOrderTimeView updateWithViewTitle:@"抢单时间" value:self.taskDetailModel.asignTime valueHeight:18];
    }
}

/// 抢单账号
- (void)initWithRobOrderAccountView {
    if (!_robOrderAccountView) {
        CGRect frame = self.robOrderTimeView.frame;
        frame.origin.y = self.robOrderTimeView.bottom;
        _robOrderAccountView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_robOrderAccountView];
    }
    if (self.taskDetailModel) {
        WDTaskFlowModel *flowModel = self.taskDetailModel.taskFlowList.firstObject;
        [_robOrderAccountView updateWithViewTitle:@"抢单账号" value:objectNull(flowModel.engineerName) valueHeight:18];
    }
}

/// 接车时间
- (void)initWithMeetCarTimeView {
    if (!_meetCarTimeView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.robOrderAccountView.bottom;
        _meetCarTimeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_meetCarTimeView];
    }
    if (self.taskDetailModel) {
        [_meetCarTimeView updateWithViewTitle:@"接车时间" value:self.taskDetailModel.receiveTime valueHeight:18];
    }
}

/// 扫码位置
- (void)initWithScanLocationView {
    WDTaskFlowModel *model = self.taskDetailModel.taskFlowList.firstObject;
    if (self.taskDetailModel.taskFlowList.count >= 2) {
        model = [self.taskDetailModel.taskFlowList objectOrNilAtIndex:1];
    }
    if (!_scanLocationView) {
        CGRect frame = self.appointTimeView.frame;
        frame.size.height = kWDTaskDetailBasicSingleViewHeight - 18 + model.scanAddressHeight;
        frame.origin.y = self.meetCarTimeView.bottom;
        _scanLocationView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
//        _scanLocationView.backgroundColor = [UIColor redColor];
        [self addSubview:_scanLocationView];
    }
    if (self.taskDetailModel) {
        
        [_scanLocationView updateWithViewTitle:@"扫码位置" value:model.scanAddress valueHeight:model.scanAddressHeight];
    }
}

/// 扫码账户
- (void)initWithScanAccountView {
    if (!_scanAccountView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.scanLocationView.bottom;
        _scanAccountView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_scanAccountView];
    }
    if (self.taskDetailModel) {
        WDTaskFlowModel *model = self.taskDetailModel.taskFlowList.firstObject;
        if (self.taskDetailModel.taskFlowList.count >= 2) {
            model = [self.taskDetailModel.taskFlowList objectOrNilAtIndex:1];
        }
        [_scanAccountView updateWithViewTitle:@"扫码账户" value:model.engineerName valueHeight:18];
    }
}

- (void)initWithLineOne {
    if (!_lineOne) {
        CGRect frame = CGRectMake(20, self.scanAccountView.bottom + 14, self.width - 40, 1);
        _lineOne = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#DDDDDD"]];
    }
    if (self.taskDetailModel) {
        _lineOne.top = self.scanAccountView.bottom + 14;
    }
}

/// 建议处理方式
- (void)initWithProcessModeView {
    if (!_processModeView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.lineOne.bottom;
        _processModeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_processModeView];
    }
    if (self.taskDetailModel) {
        [_processModeView updateWithViewTitle:@"建议处理方式" value:self.taskDetailModel.inspectFlowModel.workResult valueHeight:18];
    }
}

/// 建议处理说明
- (void)initWithProcessSpecView {
    if (!_processSpecView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.processModeView.bottom;
//        frame.size.height = 10 + height;
        _processSpecView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_processSpecView];
    }
    if (self.taskDetailModel) {
        self.processSpecView.height = 10 + self.taskDetailModel.inspectFlowModel.workExplainHeight;
        [_processSpecView updateWithViewTitle:@"建议处理说明" value:self.taskDetailModel.inspectFlowModel.workExplain valueHeight:self.taskDetailModel.inspectFlowModel.workExplainHeight];
    }
}

/// 备注图片
- (void)initWithRemarkImgTitleLabel {
    if (!_remarkImgTitleLabel) {
        CGRect frame = CGRectMake(20, self.processSpecView.bottom + 14, self.width - 40, 18);
        _remarkImgTitleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.text = @"备注图片";
        }];
    }
}

/// 车辆VIN码照片
- (void)initWithVehVinImgView {
    if (!_vehVinImgView) {
        float imageWidth = (kScreenW - 20 * 2 - 16 * 2) / 3.0;
        float imageHeight = imageWidth / 1.42857;
        float imageTitleHeight = imageHeight + 5 + 18;
        CGRect frame = CGRectMake(20, self.remarkImgTitleLabel.bottom + 14, imageWidth, imageTitleHeight);
        _vehVinImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_vehVinImgView];
        @weakify(self);
        _vehVinImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.taskDetailModel) {
        NSString *url = nil;
        if (self.taskDetailModel.inspectFlowModel && self.taskDetailModel.inspectFlowModel.files.count > 0) {
            WDInspectUploadModel *model = self.taskDetailModel.inspectFlowModel.files.firstObject;
            url = model.uploadUrl;
        }
        [self.vehVinImgView updateWithViewTitle:@"车辆VIN码照片" imageUrl:url];
    }
}

/// 车辆仪表照片
- (void)initWithVehDashImgView {
    if (!_vehDashImgView) {
        CGRect frame = self.vehVinImgView.frame;
        frame.origin.x = self.vehVinImgView.right + 16;
        _vehDashImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_vehDashImgView];
        @weakify(self);
        _vehDashImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.taskDetailModel) {
        NSString *url = nil;
        if (self.taskDetailModel.inspectFlowModel && self.taskDetailModel.inspectFlowModel.files.count > 0) {
            WDInspectUploadModel *model = self.taskDetailModel.inspectFlowModel.files[1];
            url = model.uploadUrl;
        }
        [self.vehDashImgView updateWithViewTitle:@"车辆仪表照片" imageUrl:url];
    }
}

/// 车头45°照片
- (void)initWithVehHeaderImgView {
    if (!_vehHeaderImgView) {
        CGRect frame = self.vehVinImgView.frame;
        frame.origin.x = self.vehDashImgView.right + 16;
        _vehHeaderImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_vehHeaderImgView];
        @weakify(self);
        _vehHeaderImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.taskDetailModel) {
        NSString *url = nil;
        if (self.taskDetailModel.inspectFlowModel && self.taskDetailModel.inspectFlowModel.files.count > 0) {
            WDInspectUploadModel *model = self.taskDetailModel.inspectFlowModel.files[2];
            url = model.uploadUrl;
        }
        [self.vehHeaderImgView updateWithViewTitle:@"车头45°照片" imageUrl:url];
    }
}

/// 故障部位照片
- (void)initWithVehFaultyImgView {
    if (!_vehFaultyImgView) {
        CGRect frame = self.vehVinImgView.frame;
        frame.origin.x = 20;
        frame.origin.y = self.vehHeaderImgView.bottom + 10;
        _vehFaultyImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_vehFaultyImgView];
        @weakify(self);
        _vehFaultyImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.taskDetailModel) {
        NSString *url = nil;
        if (self.taskDetailModel.inspectFlowModel && self.taskDetailModel.inspectFlowModel.files.count > 0) {
            WDInspectUploadModel *model = self.taskDetailModel.inspectFlowModel.files[3];
            url = model.uploadUrl;
        }
        [self.vehFaultyImgView updateWithViewTitle:@"故障部位照片" imageUrl:url];
    }
}

/// 驾驶证信息照片
- (void)initWithDrivingLicenceImgView {
    if (!_drivingLicenceImgView) {
        CGRect frame = _vehFaultyImgView.frame;
        frame.origin.x = _vehFaultyImgView.right + 16;
        _drivingLicenceImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_drivingLicenceImgView];
        @weakify(self);
        _drivingLicenceImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.taskDetailModel) {
        NSString *url = nil;
        if (self.taskDetailModel.inspectFlowModel && self.taskDetailModel.inspectFlowModel.files.count > 0) {
            WDInspectUploadModel *model = self.taskDetailModel.inspectFlowModel.files[4];
            url = model.uploadUrl;
        }
        [self.drivingLicenceImgView updateWithViewTitle:@"驾驶证信息照片" imageUrl:url];
    }
}

/// 其它
- (void)initWithOtherImageVideoView {
    if (!_otherImageVideoView) {
        CGRect frame = CGRectMake(0, self.drivingLicenceImgView.bottom + 10, self.width, 0);
        _otherImageVideoView = [[WDWorderOrdMainVehOtherImageVideoView alloc] initWithFrame:frame];
        [self addSubview:_otherImageVideoView];
        @weakify(self);
        _otherImageVideoView.touchPlayVideoBlock = ^(WDInspectUploadModel * _Nonnull model) {
            @strongify(self);
//            debugLog(@"点儿我让");
//            if (self.touchPlayVideoBlock) {
//                self.touchPlayVideoBlock(model);
//            }
        };
    }
    if (self.taskDetailModel) {
//        @weakify(self);
        if (self.taskDetailModel.inspectFlowModel && self.taskDetailModel.inspectFlowModel.otherFiles.count > 0) {
            self.otherImageVideoView.hidden = NO;
        } else {
            self.otherImageVideoView.hidden = YES;
        }
        self.otherImageVideoView.height = self.taskDetailModel.inspectFlowModel.otherFilesHeight;
        [_otherImageVideoView updateWithViewTitle:@"其他照片/视频" images:self.taskDetailModel.inspectFlowModel.otherFiles playVideoBlock:nil];
    }
}

/// 处理时间
- (void)initWithProcessTimeView {
    if (!_processTimeView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.otherImageVideoView.bottom;
        _processTimeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_processTimeView];
    }
    if (self.taskDetailModel) {
        [_processTimeView updateWithViewTitle:@"处理时间" value:self.taskDetailModel.inspectFlowModel.createTime valueHeight:18];
    }
}

/// 处理账号
- (void)initWithProcessAccountView {
    if (!_processAccountView) {
        CGRect frame = self.appointTimeView.frame;
        frame.origin.y = self.processTimeView.bottom;
        _processAccountView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_processAccountView];
    }
    if (self.taskDetailModel) {
        [_processAccountView updateWithViewTitle:@"处理账号" value:self.taskDetailModel.inspectFlowModel.engineerName valueHeight:18];
    }
}

- (void)browseImageView:(NSInteger)index view:(UIImageView *)view picList:(NSArray *)picList {
    // 图片预览
    HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
    browser.isNeedLandscape = NO;
    browser.imageArray = [picList copy];
    browser.currentImageIndex = (int)index;
    browser.imageCount = picList.count;
    if(picList.count == 1)//如果是一张图片tag 是200
    {
        browser.sourceImagesContainerView = view;
        browser.startTag = 200;
    }
    else//如果是多张图片tag 从100开始
    {
        browser.startTag = 100;
        browser.sourceImagesContainerView = view;
    }
    [browser show];
}

- (void)updateWithViewData:(id)data {
    self.taskDetailModel = data;
    
    /// 预约时间
    [self initWithAppointTimeView];
    
    // 抢单时间
    [self initWithRobOrderTimeView];
    
    // 抢单账号
    [self initWithRobOrderAccountView];

    /// 接车时间
    [self initWithMeetCarTimeView];

    /// 扫码位置
    [self initWithScanLocationView];

    /// 扫码账户
    [self initWithScanAccountView];
    
    if (self.taskDetailModel.inspectFlowModel) {
        [self initWithLineOne];
        
        /// 建议处理方式
        [self initWithProcessModeView];

        /// 建议处理说明
        [self initWithProcessSpecView];
        
        /// 备注图片
        [self initWithRemarkImgTitleLabel];
        
        /// 车辆VIN码照片
        [self initWithVehVinImgView];

        /// 车辆仪表照片
        [self initWithVehDashImgView];

        /// 车头45°照片
        [self initWithVehHeaderImgView];

        /// 故障部位照片
        [self initWithVehFaultyImgView];

        /// 驾驶证信息照片
        [self initWithDrivingLicenceImgView];
        
        /// 其它
        [self initWithOtherImageVideoView];
        
        /// 处理时间
        [self initWithProcessTimeView];

        /// 处理账号
        [self initWithProcessAccountView];
    }
    
    

}

@end
